import router from "@/router";
import {useLoginUserStore} from "@/store/useLoginUserStore.ts";
import {message} from "ant-design-vue";


router.beforeEach((to, from, next) => {
  const loginUserStore = useLoginUserStore();
  const loginUser = loginUserStore.loginUser;
  const toUrl = to.fullPath;
  const loginWhiteList = ['/user/login', '/user/register'];

  console.log(loginUser)
  if (loginWhiteList.includes(toUrl)) {
    next();
    return;
  } else if (toUrl.startsWith('/admin')) {
    console.log("admin")
    if (!loginUser || loginUser.userRole !== 1) {
      message.error("无权限");
      next(`/user/login?redirect=${to.fullPath}`);
      return;
    }else{
      next();
    }
  } else {
    console.log("loginUser")
    if(!loginUser.id){
      message.error("请先登录");
      next(`/user/login`);
      return;
    }else{
      next();
    }
  }

})
